//import 'dart:html';

import 'package:shortmovieapp/Common.dart';
import 'package:shortmovieapp/data/localData.dart';
import 'package:shortmovieapp/data/viewData.dart';
import 'package:shortmovieapp/http/core/http_response.dart';
import 'dart:math';

import 'package:shortmovieapp/http/request/appRequest.dart';

class RequestServerController {
//单例
  static RequestServerController? _instance;

  RequestServerController._();

  factory RequestServerController() {
    _instance ??= RequestServerController._();
    return _instance!;
  }

  //获取推荐影片列表
  Future<List<TypeMovieItems>> getRecommendedMoviesData(List<MovieType> types)
   async {
    List<TypeMovieItems> list = [];
    for (int i = 0; i < types.length; i++) {
      var req = getRecommendedMoviesRequest(types[i], 10);
      HttpResponse response = await req.send();
      if(!response.suc){
        Debug.log("fetch fail code:${response.code}");
      }
      else
      {
       if(response.data != null)
       { 
          String responseData = response.data!;
          //Debug.log("getRecommendedMoviesRequest:${response.data}");
          TypeMovieItems value = TypeMovieItems.fromJsonString(responseData);
          Debug.log("itemDatas:${value.itemDatas?.length}");
          list.add(value);
       }
       else{
          Debug.log("value is null,response.data:${response.data}");
       }
      }
    }
    
    // var list = List<TypeMovieItems>.generate(types.length, (index) {
    //   return TypeMovieItems(testTypeMovieItems(types[index], 9));
    // });
    return list;
  }

  //获取搜索影片列表
  List<MovieItemData> getSearchMovies(String searchKey) {
    int count = 15;
    List<MovieItemData> list = [];
    for (int i = 0; i < count; i++) {
      var typeStr = MovieType2String(
          MovieType.values[Random().nextInt(MovieType.values.length - 1) + 1]);

      list.add(testMovieItemData(i, "搜索影片${i + 1}", typeStr));
    }
    return list;
  }

  //获取逛逛视频列表
  List<VideoData> fetchWanderVideos() {
    return testVideos();
  }

  //请求视频数据
  Future<MovieData?> getShortMovieData(String movieName)async {

      var req = getMovieDataRequest(movieName);
      HttpResponse response = await req.send();
      if(!response.suc){
        Debug.log("fetch fail code:${response.code}");
        return null;
      }
      else
      {
        if(response.data != null)
        { 
            String responseData = response.data!;
            var value = MovieData.fromJsonString(responseData);
            Debug.log("MovieData:$value");
            return value;
        }
        else{
            Debug.log("value is null,response.data:${response.data}");
            return null;
        }
      }
  }
  MovieData testShortMovieData(int movieId)
   {
    //int index = movieId - 1;
    //index = index<testVideoList.length?index:0;
    int episodeCount = testVideoList.length;

    List<VideoData> list = [];
    for (int i = 0; i < episodeCount; i++) {
      list.add(VideoData(
          movieId: movieId,
          episodeId: i + 1,
          url: 'https://static.ybhospital.net/${testVideoList[i]}',
          icon: '',publishedDate: getDateTime(),approved: true));
    }
    return MovieData(testMovieItemData(movieId,
        "test",MovieType.fanzhuan.stringValue),list);
  }

  MovieItemData testMovieItemData(int id, String title, String type) {
    return MovieItemData(
        id,
        title,
        "https://img0.baidu.com/it/u=3345274712,2160003303&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=741",
        10,
        type,
        10000,"");
  }

  List<MovieItemData> testTypeMovieItems(MovieType type, int count) {
    List<MovieItemData> list = [];
    for (int i = 0; i < count; i++) {
      var typeStr = MovieType2String(type);
      if (type == MovieType.recommond) {
        typeStr = MovieType2String(MovieType
            .values[Random().nextInt(MovieType.values.length - 1) + 1]);
      }

      list.add(testMovieItemData(i + 1, "${typeStr}影片${i + 1}", typeStr));
    }
    return list;
  }

  List<String> testVideoList = [
    'test-video-10.MP4',
    'test-video-6.mp4',
    'test-video-9.MP4',
    'test-video-8.MP4',
    'test-video-7.MP4',
    'test-video-1.mp4',
    'test-video-2.mp4',
    'test-video-3.mp4',
    'test-video-4.mp4',
  ];
  List<VideoData> testVideos() {
    List<VideoData> list = testVideoList
        .map((e) => VideoData(
            movieId: 1,
            episodeId: 1,
            icon: '',
            url: 'https://static.ybhospital.net/$e',
            title: '$e',publishedDate: getDateTime(),approved: true))
        .toList();
    return list;
  }
}
